//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverSetupTakeOver.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_instance_id","_action_id"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);
_action_id = [_action] call FFUNC(actionGetID);

if (!isnull _object) then {
	_evt_idx = _object addEventHandler ["killed", Format["[_this, %1] call %2;",_action_id,QUOTE(FFUNC(eventObjectiveTakeOverKilled))]];
	[_object,_action,"TAKEOVER_KILLED_EVENT",_evt_idx] call FFUNC(actionSetLocal);

	private ["_object","_instance_id","_entities","_taken","_hold_count","_count_owners","_count_takers","_count_soldiers","_action","_parameters","_zone_size","_takenover_msg","_lost_msg","_shared_takeover","_hold_time"
			,"_action_id","_owner_side","_taker_side","_takenover_code","_lost_code","_evt_idx","_share_dist","_grp","_leader","_grps","_wp","_taken_time"];
	DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
	DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
	DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);
	_action_id = [_action] call FFUNC(actionGetID);
	_base_conquer_timeout = ["BASE_CONQUERED_TIMEOUT",15] call FFUNC(getFrameworkValue);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);
	_zone_size = [_parameters, "ZONE_SIZE", 30,THIS_FILE] call FFUNC(hashGet);
	_takenover_msg = [_parameters, "TAKEN_MSG", "",THIS_FILE] call FFUNC(hashGet);
	_lost_msg = [_parameters, "LOST_MSG", "",THIS_FILE] call FFUNC(hashGet);
	_shared_takeover = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
	_hold_time = [_parameters, "HOLD_TIME", _base_conquer_timeout,THIS_FILE] call FFUNC(hashGet);
	_owner_side = [_parameters, "OWNER_SIDE", GVAR(side_enemy),THIS_FILE] call FFUNC(hashGet);
	_taker_side = [_parameters, "TAKER_SIDE", GVAR(side_own),THIS_FILE] call FFUNC(hashGet);
	_takenover_code = [_parameters, "TAKEN_CODE", "",THIS_FILE] call FFUNC(hashGet);
	_lost_code = [_parameters, "LOST_CODE", "",THIS_FILE] call FFUNC(hashGet);
	_share_dist = [_parameters, "SHARED_DIST", 300,THIS_FILE] call FFUNC(hashGet);
	_shared_takeover = _shared_takeover call FFUNC(getStringVar);

	if (typename _takenover_msg == typename "") then {_takenover_msg = [_takenover_msg];};
	if (typename _lost_msg == typename "") then {_lost_msg = [_lost_msg];};
	_taken = false;
	_taken_time = 0;
	if (!isnil "KRON_UPS_sharedist") then { if (KRON_UPS_sharedist > _share_dist)  then { _share_dist = KRON_UPS_sharedist; }; };

	if (!isNil "_shared_takeover") then {
		if (_shared_takeover != "") then {
			[_instance_id,_shared_takeover,_taken] spawn FFUNC(setMissionJIPVar);
			[QGVAR(all_var), [_instance_id,_shared_takeover,_taken],THIS_FILE] call FFUNC(callEvent);
		};
	};

	if (typename _owner_side == typename []) then {
		for "_i" from 0 to ((count _owner_side) - 1) step 1 do {
			if (typename (_owner_side select _i) == typename "") then {
				_owner_side set [_i, (switch {toupper((_owner_side select _i))} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_enemy)};	})];
			};
		};
	} else {
		if (typename _owner_side == typename "") then {
			_owner_side = switch {toupper(_owner_side)} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_enemy)};	};
		};
		_owner_side = [_owner_side];
	};
	if (typename _taker_side == typename []) then {
		for "_i" from 0 to ((count _taker_side) - 1) step 1 do {
			if (typename (_taker_side select _i) == typename "") then {
				_taker_side set [_i, (switch {toupper((_taker_side select _i))} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_own)};	})];
			};
		};
	} else {
		if (typename _taker_side == typename "") then {
			_taker_side = switch {toupper(_taker_side)} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_own)};	};
		};
		_taker_side = [_taker_side];
	};
	if (_share_dist > 300) then {_share_dist = 300;};
	
	_uid = ["UID"] call mso_fnc_fsmloop;
	_object setVariable [format["%1_%2",QGVAR(Hold_Count),_action_id],0];
	_object setVariable [format["%1_%2",QGVAR(Taken),_action_id],false];
	_object setVariable [format["%1_%2",QGVAR(Taken_Time),_action_id],time];
	_object setVariable [format["%1_%2",QGVAR(End_Check),_action_id],false];
	["itemAdd", [_uid, QUOTE(FFUNC(serverSetupTakeOverExec)),[_object,_action_id,_instance_id,_zone_size,_hold_time,_owner_side,_taker_side,_share_dist],3.57,"seconds",QUOTE(FFUNC(serverSetupTakeOverCheck))]] call mso_fnc_fsmloop;
};
TRACE("End");
TMFPROFILERSTOP;